package com.oreilly.ez.service.impl;

import com.oreilly.ez.entity.CourseTerm;
import com.oreilly.ez.repo.CourseTermRepo;
import com.oreilly.ez.service.CourseTermService;
import com.oreilly.ez.util.CloneUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class CourseTermServiceImpl  implements CourseTermService{


    @Autowired
    private CourseTermRepo courseTermRepo;

    @Override
    public CourseTerm save(CourseTerm item) {

        // is update ??
        Integer sid = item.getSid();
        if(sid != null){
            CourseTerm dbItem = courseTermRepo.getOne(sid);

            CloneUtils.copyProperties(item, dbItem);
            CloneUtils.copyProperties(dbItem, item);

        }

        return courseTermRepo.save(item);

    }

    @Override
    public void remove(Integer sid) {

        courseTermRepo.deleteById(sid);
    }


    @Override
    public Page<CourseTerm> find(CourseTerm item, Pageable pageable) {

        return courseTermRepo.findAll(pageable);
//        Integer sid = item.getCourse().getSid();
//        return courseTermRepo.findByCourseId(sid, pageable);
    }

    @Override
    public CourseTerm findOne(Integer sid) {

        return courseTermRepo.findById(sid).get();
    }


}
